/*
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2023/10/25 15:40
@File     : sid.go
@Project  : GoProject
@Desc     :
*/
package sid

import (
	"github.com/bwmarrin/snowflake"
	"github.com/spf13/viper"
	"time"
)

type Sid struct {
	sn *snowflake.Node
}

func NewSid(conf *viper.Viper) *Sid {
	//设置起始时间
	st, err := time.Parse("2006-01-02", conf.GetString("snowflake.start_time"))
	if err != nil {
		panic(err)
	}
	snowflake.Epoch = st.Unix()
	sn, err := snowflake.NewNode(conf.GetInt64("snowflake.machine_id"))
	return &Sid{sn: sn}
}

func (s Sid) GenId() int64 {
	return s.sn.Generate().Int64()
}
